package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Autowired
	private BaseAttributeValueMapper baseAttributeValueMapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;




	@Override
	public List<JSONObject> getBaseCategoryList() {
		//查询三级分类id 一次性全部查出来 然后 分组过滤 封装
		//去视图中查询

		List<JSONObject> JSONObjects = new ArrayList<>();


		//1、通过视图获取所有分类信息 包括了 1 2 3 级分类
		List<BaseCategoryView> baseCategoryViews1 = baseCategoryViewMapper.selectList(null);

		if (CollectionUtils.isEmpty(baseCategoryViews1)){
			return JSONObjects;
		}


		//2、根据一级id分组
		Map<Long, List<BaseCategoryView>> baseCategoryViews1group = baseCategoryViews1.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		/*
		*
		*     "categoryName":"图书、音像、电子书刊",   #一级分类名称
    			"categoryId":1,                       #一级分类ID
    			"categoryChild":[                     #当前一级分类包含的二级分类集合
		* */
		//根据二级id分组 将二级分类封装到categoryChild
		baseCategoryViews1group.forEach((categoryId, baseCategoryView2) -> {
			JSONObject jsonObject1 = new JSONObject();
			JSONObjects.add(jsonObject1);
			jsonObject1.put("categoryName", baseCategoryView2.get(0).getCategory1Name());
			jsonObject1.put("categoryId", categoryId);

			List<JSONObject> JSONObjects2 = new ArrayList<>();
			jsonObject1.put("categoryChild", JSONObjects2);
			//根据二级id分组

/*			"categoryName":"电子书刊",     #二级分类名称
			"categoryId":1,               #二级分类ID
			"categoryChild":[             #当前二级分类包含的三级分类集合*/
			baseCategoryView2.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id))
					.forEach((categoryId2,baseCategoryViews3)->{
						JSONObject jsonObject2 = new JSONObject();
						JSONObjects2.add(jsonObject2);
						jsonObject2.put("categoryName", baseCategoryViews3.get(0).getCategory2Name());
						jsonObject2.put("categoryId", categoryId2);

						//组装三级分类
//						"categoryName":"电子书",#三级分类名称
//						"categoryId":1         #三级分类ID
						jsonObject2.put("categoryChild",baseCategoryViews3.stream().map((baseCategoryView3)->{


							JSONObject jsonObject3 = new JSONObject();
							jsonObject3.put("categoryId", baseCategoryView3.getCategory3Id());
							jsonObject3.put("categoryName", baseCategoryView3.getCategory3Name());
							return jsonObject3;
						}).collect(Collectors.toList()));

					});

		});


		return JSONObjects;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long categoryId) {


	//第一种做法 分布查
		//根据一级分类id 查询标签
/*		List<BaseAttribute> baseAttributes = baseAttributeMapper.selectList(new LambdaQueryWrapper<BaseAttribute>()
				.eq(BaseAttribute::getCategory1Id, categoryId));

		baseAttributes.forEach((baseAttribute -> {
			baseAttribute.setAttributeValueList(baseAttributeValueMapper.selectList(new LambdaQueryWrapper<BaseAttributeValue>()
					.eq(BaseAttributeValue::getAttributeId,baseAttribute.getId())));
		}));*/
	//第二种做法 一次性查完
		List<BaseAttribute>baseAttributes = baseAttributeMapper.selectListBycategory1Id(categoryId);


		return baseAttributes;
	}

    @Override
    public BaseCategoryView getBaseCategoryViewBycategory3Id(Long category3Id) {


	return this.baseCategoryViewMapper.selectById(category3Id);

    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        //根据一级id查询2级di
		List<BaseCategory2> baseCategory2s = this.baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
		if (CollectionUtils.isEmpty(baseCategory2s)){
			return null;
		}
			//获取二级分类id集合
		List<Long> category2Ids = baseCategory2s.stream().map(BaseCategory2::getId).collect(Collectors.toList());

		//根据二级分类id集合查询三级分类

		return this.baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().in(BaseCategory3::getCategory2Id,category2Ids)
				.last("limit 7")
				.select(BaseCategory3::getId,BaseCategory3::getName)
		);

	}

	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		List<BaseCategoryView> baseCategoryViews = this.baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));

		JSONObject jsonObject1 = new JSONObject();

		jsonObject1.put("categoryName", baseCategoryViews.get(0).getCategory1Name());
		jsonObject1.put("categoryId", category1Id);

		List<JSONObject> JSONObjects2 = new ArrayList<>();
		jsonObject1.put("categoryChild", JSONObjects2);
		//根据二级id分组

/*			"categoryName":"电子书刊",     #二级分类名称
			"categoryId":1,               #二级分类ID
			"categoryChild":[             #当前二级分类包含的三级分类集合*/
		baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id))
				.forEach((categoryId2,baseCategoryViews3)->{
					JSONObject jsonObject2 = new JSONObject();
					JSONObjects2.add(jsonObject2);
					jsonObject2.put("categoryName", baseCategoryViews3.get(0).getCategory2Name());
					jsonObject2.put("categoryId", categoryId2);

					//组装三级分类
//						"categoryName":"电子书",#三级分类名称
//						"categoryId":1         #三级分类ID
					jsonObject2.put("categoryChild",baseCategoryViews3.stream().map((baseCategoryView3)->{


						JSONObject jsonObject3 = new JSONObject();
						jsonObject3.put("categoryId", baseCategoryView3.getCategory3Id());
						jsonObject3.put("categoryName", baseCategoryView3.getCategory3Name());
						return jsonObject3;
					}).collect(Collectors.toList()));

				});
		return jsonObject1;
	}
}
